package cn.xgx.web.controller.backstage.user;

import cn.xgx.model.User;
import cn.xgx.result.ResponseData;
import cn.xgx.service.UserService;
import cn.xgx.web.model.input.UserQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "用户信息")
@RestController
@RequestMapping(value = "/api/backstage/usermanage")
@Slf4j
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public ResponseData queryPage(@RequestBody UserQuery userQuery){
        Page<User> pageQuery = new Page<>(userQuery.getCurrent(),userQuery.getSize());
        return ResponseData.success(userService.page(pageQuery));
    }

    @GetMapping("/{id}")
    public ResponseData get(@PathVariable Long id){
        return ResponseData.success(userService.getById(id));
    }

    @PostMapping
    public ResponseData save(@RequestBody User user){
        return ResponseData.success(userService.save(user));
    }

    @PutMapping
    public ResponseData update(@RequestBody User user){
        return ResponseData.success(userService.updateById(user));
    }

    @DeleteMapping("/{ids}")
    public ResponseData delete(@PathVariable List<Long> ids){
        // 关于mybatis-plus的removeByIds说明
        // 1.只删除一条记录：id在数据库不存在，userService.removeByIds(ids)也会返回false，表示删除失败。
        // 2.删除多条记录：只要有一条记录删除成功，都会返回true
        return ResponseData.success(userService.removeByIds(ids));
    }

}
